<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="需求项目中有时会遇到监听用户截屏操作的需求，应用场景也比较丰富。 eg. 1: 某个隐私页面禁止截屏，当发现用户截屏后，删除该截屏文件； eg. 2: 当用户截屏时，可能是需要进行分享，意见反馈等操作，此时进行弹窗提示，方便用户操作（支付宝就有类似功能）。 分析安卓系统里面并没有提供监听截屏的接口，所以就需要自己去实现（不然也不会有本文）。  一般来说，对于某一操作的监听，第一直觉就是监听对应按键">
<meta property="og:type" content="article">
<meta property="og:title" content="详细讲解安卓截屏监听操作">
<meta property="og:url" content="http://vchao.top/2021/08/25/%E5%AE%89%E5%8D%93%E6%88%AA%E5%B1%8F%E7%9B%91%E5%90%AC/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="需求项目中有时会遇到监听用户截屏操作的需求，应用场景也比较丰富。 eg. 1: 某个隐私页面禁止截屏，当发现用户截屏后，删除该截屏文件； eg. 2: 当用户截屏时，可能是需要进行分享，意见反馈等操作，此时进行弹窗提示，方便用户操作（支付宝就有类似功能）。 分析安卓系统里面并没有提供监听截屏的接口，所以就需要自己去实现（不然也不会有本文）。  一般来说，对于某一操作的监听，第一直觉就是监听对应按键">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-25T14:42:51.174Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.802Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="截屏">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://vchao.top/2021/08/25/%E5%AE%89%E5%8D%93%E6%88%AA%E5%B1%8F%E7%9B%91%E5%90%AC/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>详细讲解安卓截屏监听操作 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/%E5%AE%89%E5%8D%93%E6%88%AA%E5%B1%8F%E7%9B%91%E5%90%AC/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          详细讲解安卓截屏监听操作
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><p>项目中有时会遇到监听用户截屏操作的需求，应用场景也比较丰富。</p>
<p>eg. 1: 某个隐私页面禁止截屏，当发现用户截屏后，删除该截屏文件；</p>
<p>eg. 2: 当用户截屏时，可能是需要进行分享，意见反馈等操作，此时进行弹窗提示，方便用户操作（支付宝就有类似功能）。</p>
<h2 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h2><p>安卓系统里面并没有提供监听截屏的接口，所以就需要自己去实现（不然也不会有本文）。</p>
<ul>
<li><p>一般来说，对于某一操作的监听，第一直觉就是监听对应按键的点击事件。但是各种定制系统的截图方式五花八门，对截图按键的监听并不容易。</p>
<blockquote>
<p> 截图方式 ：</p>
<p>eg.1  快捷组合键截屏 ：【电源键】+【音量-】；【home键】+【音量-】；【电源键】+【Home键】……</p>
<p>eg.2 快捷菜单的截屏按键</p>
<p>eg.3 借助其它软件截屏：例如QQ辅助功能中的摇一摇截屏；豌豆荚，360手机助手等通过usb调试的截屏 ……</p>
<p>eg.4 定制系统的其它截屏方式：三指下滑截屏 ；长按指纹键截屏 ……</p>
</blockquote>
<p>综上，此路不通。</p>
</li>
<li><p>既然无法通过按键点击判断出截屏操作，那就只好在生成截屏图片后进行判断。</p>
<p>也就是<strong>【对图库进行监听】</strong>，如果 <strong>【特定时间】</strong>增加了 **【符合规则的图片】 **，则判定用户进行了截图操作。</p>
</li>
</ul>
<h2 id="具体操作"><a href="#具体操作" class="headerlink" title="具体操作"></a>具体操作</h2><h3 id="1-权限"><a href="#1-权限" class="headerlink" title="1. 权限"></a>1. 权限</h3><p>因为对图库文件进行监听，需要用到内存读的权限，所以需要在 AndroidManifest.xml 中添加权限:</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.READ_EXTERNAL_STORAGE&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p>内存读写权限属于高危权限，在安卓6.0及以上的系统还需要进行动态权限的申请。在对应页面的Activity中进行权限判断以及申请。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (Build.VERSION.SDK_INT &gt; <span class="number">22</span>) &#123;</span><br><span class="line">    List&lt;String&gt; permissionList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    <span class="comment">// 检查是否有内存权限</span></span><br><span class="line">    <span class="keyword">if</span> (ContextCompat.checkSelfPermission(ScreenshotsActivity.<span class="keyword">this</span>, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) &#123;</span><br><span class="line">        permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 开始截图监听</span></span><br><span class="line">        doStart();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (permissionList != <span class="keyword">null</span> &amp;&amp; (permissionList.size() != <span class="number">0</span>)) &#123;</span><br><span class="line">        ActivityCompat.requestPermissions(ScreenshotsActivity.<span class="keyword">this</span>, permissionList.toArray(<span class="keyword">new</span> String[permissionList.size()]), <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-创建截图监听器"><a href="#2-创建截图监听器" class="headerlink" title="2. 创建截图监听器"></a>2. 创建截图监听器</h3><p>创建截图监听器文件，当判断用户进行截图操作之后，以接口回调的方式进行处理。</p>
<blockquote>
<p>那么如何判断进行了截图操作呢？这就又回归到分析中的结论：</p>
<p>​    【监听图库】，若 【特定时间内】增加【符合规则的图片】，则判定进行了截图操作。</p>
</blockquote>
<h4 id="2-1-监听图库"><a href="#2-1-监听图库" class="headerlink" title="2.1 监听图库"></a>2.1 监听图库</h4><p>我们要对图库进行监听，需要读取媒体数据库。那么需要监听读取的列如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 读取媒体数据库时需要读取的列</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] MEDIA_PROJECTIONS = &#123;</span><br><span class="line">    MediaStore.Images.ImageColumns.DATA,</span><br><span class="line">        MediaStore.Images.ImageColumns.DATE_TAKEN,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 读取媒体数据库时需要读取的列, 其中 WIDTH 和 HEIGHT 字段在 API 16 以后才有</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] MEDIA_PROJECTIONS_API_16 = &#123;</span><br><span class="line">    MediaStore.Images.ImageColumns.DATA,</span><br><span class="line">    MediaStore.Images.ImageColumns.DATE_TAKEN,</span><br><span class="line">    MediaStore.Images.ImageColumns.WIDTH,</span><br><span class="line">    MediaStore.Images.ImageColumns.HEIGHT,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p> 图库监听代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">Cursor cursor = <span class="keyword">null</span>;</span><br><span class="line">    <span class="comment">// 数据改变时查询数据库中最后加入的一条数据</span></span><br><span class="line">    cursor = mContext.getContentResolver().query(</span><br><span class="line">            contentUri,</span><br><span class="line">            Build.VERSION.SDK_INT &lt; <span class="number">16</span> ? MEDIA_PROJECTIONS : MEDIA_PROJECTIONS_API_16,</span><br><span class="line">            <span class="keyword">null</span>,</span><br><span class="line">            <span class="keyword">null</span>,</span><br><span class="line">            MediaStore.Images.ImageColumns.DATE_ADDED + <span class="string">&quot; desc limit 1&quot;</span></span><br><span class="line">    );</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (cursor == <span class="keyword">null</span>) &#123;</span><br><span class="line">        LogUtils.e(<span class="string">&quot;Deviant logic.&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (!cursor.moveToFirst()) &#123;</span><br><span class="line">        LogUtils.e(<span class="string">&quot;Cursor no data.&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取各列的索引</span></span><br><span class="line">    <span class="keyword">int</span> dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);</span><br><span class="line">    <span class="keyword">int</span> dateTakenIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN);</span><br><span class="line">    <span class="keyword">int</span> widthIndex = -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">int</span> heightIndex = -<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (Build.VERSION.SDK_INT &gt;= <span class="number">16</span>) &#123;</span><br><span class="line">        widthIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.WIDTH);</span><br><span class="line">        heightIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.HEIGHT);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取行数据</span></span><br><span class="line">    String data = cursor.getString(dataIndex);</span><br><span class="line">    <span class="keyword">long</span> dateTaken = cursor.getLong(dateTakenIndex);</span><br><span class="line">    <span class="keyword">int</span> width = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> height = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (widthIndex &gt;= <span class="number">0</span> &amp;&amp; heightIndex &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">        width = cursor.getInt(widthIndex);</span><br><span class="line">        height = cursor.getInt(heightIndex);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// API 16 之前, 宽高要手动获取</span></span><br><span class="line">        Point size = getImageSize(data);</span><br><span class="line">        width = size.x;</span><br><span class="line">        height = size.y;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 处理获取到的第一行数据</span></span><br><span class="line">    handleMediaRowData(data, dateTaken, width, height);</span><br></pre></td></tr></table></figure>

<h4 id="2-2-特定时间内"><a href="#2-2-特定时间内" class="headerlink" title="2.2 特定时间内"></a>2.2 特定时间内</h4><p>所谓特定时间内，即为监听增加开始和结束的方法。可以通过两个方法进行时间的控制。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 启动监听</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">startListen</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    assertInMainThread();</span><br><span class="line"></span><br><span class="line">    sHasCallbackPaths.clear();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 记录开始监听的时间戳</span></span><br><span class="line">    mStartListenTime = System.currentTimeMillis();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建内容观察者</span></span><br><span class="line">    mInternalObserver = <span class="keyword">new</span> MediaContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI, mUiHandler);</span><br><span class="line">    mExternalObserver = <span class="keyword">new</span> MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mUiHandler);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注册内容观察者</span></span><br><span class="line">    mContext.getContentResolver().registerContentObserver(</span><br><span class="line">            MediaStore.Images.Media.INTERNAL_CONTENT_URI,</span><br><span class="line">            <span class="keyword">false</span>,</span><br><span class="line">            mInternalObserver</span><br><span class="line">    );</span><br><span class="line">    mContext.getContentResolver().registerContentObserver(</span><br><span class="line">            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,</span><br><span class="line">            <span class="keyword">false</span>,</span><br><span class="line">            mExternalObserver</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 停止监听</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stopListen</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    assertInMainThread();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注销内容观察者</span></span><br><span class="line">    <span class="keyword">if</span> (mInternalObserver != <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">          mContext.getContentResolver().unregisterContentObserver(mInternalObserver);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        mInternalObserver = <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (mExternalObserver != <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">          mContext.getContentResolver().unregisterContentObserver(mExternalObserver);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        mExternalObserver = <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 清空数据</span></span><br><span class="line">    mStartListenTime = <span class="number">0</span>;</span><br><span class="line">    sHasCallbackPaths.clear();</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="2-3-符合规则的图片"><a href="#2-3-符合规则的图片" class="headerlink" title="2.3 符合规则的图片"></a>2.3 符合规则的图片</h4><p>判断是否符合规则，首先要先定制一个规则。判断截图，一般可以从以下几个方面进行判断：</p>
<ul>
<li><p>时间判断：</p>
<ul>
<li><p>也就是我们2.2中所说的特定时间内，如果图片的创建时间不在其中，则不认定为截图；</p>
</li>
<li><p>为了提高容错性，也可以对时间限制进行适当放松；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 如果加入数据库的时间在开始监听之前, 或者与当前时间相差大于10秒, 则认为当前没有截屏</span></span><br><span class="line"><span class="keyword">if</span> (dateTaken &lt; mStartListenTime || (System.currentTimeMillis() - dateTaken) &gt; <span class="number">10</span> * <span class="number">1000</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>尺寸判断：</p>
<ul>
<li><p>屏幕是有固定分辨率的，一般来说，屏幕截图的分辨率也应该小于屏幕的分辨率；</p>
</li>
<li><p>但是如果是长截图，则图片的高度会超出屏幕；（是否判断长截图，根据自己需求制定）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 尺寸判断</span></span><br><span class="line"><span class="keyword">if</span> (sScreenRealSize != <span class="keyword">null</span>) &#123;</span><br><span class="line">    <span class="comment">// 如果图片尺寸超出屏幕, 则认为当前没有截屏（此处没有判断长截屏）</span></span><br><span class="line">    <span class="keyword">if</span> (!((width &lt;= sScreenRealSize.x &amp;&amp; height &lt;= sScreenRealSize.y)</span><br><span class="line">            || (height &lt;= sScreenRealSize.x &amp;&amp; width &lt;= sScreenRealSize.y))) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>路径名称判断</p>
<ul>
<li><p>通过统计来看，截图文件的图片绝大多数的情况下都会包含screenshot或者类似的关键字。我们在这里进行图片路径的判断。</p>
</li>
<li><p>经过整理得出以下关键字，已经涵盖绝大多数情况的系统机型截图：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 此处忽略大小写，后续判断时，统一转换为小写即可</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] KEYWORDS = &#123;</span><br><span class="line">        <span class="string">&quot;screenshot&quot;</span>, <span class="string">&quot;screen_shot&quot;</span>, <span class="string">&quot;screen-shot&quot;</span>, <span class="string">&quot;screen shot&quot;</span>,</span><br><span class="line">        <span class="string">&quot;screencapture&quot;</span>, <span class="string">&quot;screen_capture&quot;</span>, <span class="string">&quot;screen-capture&quot;</span>, <span class="string">&quot;screen capture&quot;</span>,</span><br><span class="line">        <span class="string">&quot;screencap&quot;</span>, <span class="string">&quot;screen_cap&quot;</span>, <span class="string">&quot;screen-cap&quot;</span>, <span class="string">&quot;screen cap&quot;</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>判断是否包含关键字</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 将路径转换为小写</span></span><br><span class="line">data = data.toLowerCase();</span><br><span class="line"><span class="comment">// 判断图片路径是否含有指定的关键字之一, 如果有, 则认为当前截屏了</span></span><br><span class="line"><span class="keyword">for</span> (String keyWork : KEYWORDS) &#123;</span><br><span class="line">    <span class="keyword">if</span> (data.contains(keyWork)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h3 id="3-调用"><a href="#3-调用" class="headerlink" title="3. 调用"></a>3. 调用</h3><p>完成上述操作之后，即可在响应页面开始监听，并进行响应操作</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">manager = ScreenShotListenManager.newInstance(ScreenshotsActivity.<span class="keyword">this</span>);</span><br><span class="line">manager.setListener(</span><br><span class="line">        <span class="keyword">new</span> ScreenShotListenManager.OnScreenShotListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onShot</span><span class="params">(String imagePath)</span> </span>&#123;</span><br><span class="line">                <span class="comment">// do something            </span></span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">);</span><br></pre></td></tr></table></figure>









<h2 id="完整代码："><a href="#完整代码：" class="headerlink" title="完整代码："></a>完整代码：</h2><p>ScreenShotListenManager.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 读取媒体数据库时需要读取的列</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] MEDIA_PROJECTIONS = &#123;</span><br><span class="line">        MediaStore.Images.ImageColumns.DATA,</span><br><span class="line">        MediaStore.Images.ImageColumns.DATE_TAKEN,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 读取媒体数据库时需要读取的列, 其中 WIDTH 和 HEIGHT 字段在 API 16 以后才有</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] MEDIA_PROJECTIONS_API_16 = &#123;</span><br><span class="line">        MediaStore.Images.ImageColumns.DATA,</span><br><span class="line">        MediaStore.Images.ImageColumns.DATE_TAKEN,</span><br><span class="line">        MediaStore.Images.ImageColumns.WIDTH,</span><br><span class="line">        MediaStore.Images.ImageColumns.HEIGHT,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 截屏依据中的路径判断关键字</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String[] KEYWORDS = &#123;</span><br><span class="line">        <span class="string">&quot;screenshot&quot;</span>, <span class="string">&quot;screen_shot&quot;</span>, <span class="string">&quot;screen-shot&quot;</span>, <span class="string">&quot;screen shot&quot;</span>,</span><br><span class="line">        <span class="string">&quot;screencapture&quot;</span>, <span class="string">&quot;screen_capture&quot;</span>, <span class="string">&quot;screen-capture&quot;</span>, <span class="string">&quot;screen capture&quot;</span>,</span><br><span class="line">        <span class="string">&quot;screencap&quot;</span>, <span class="string">&quot;screen_cap&quot;</span>, <span class="string">&quot;screen-cap&quot;</span>, <span class="string">&quot;screen cap&quot;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> Point sScreenRealSize;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 已回调过的路径</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> List&lt;String&gt; sHasCallbackPaths = <span class="keyword">new</span> ArrayList&lt;String&gt;();</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> Context mContext;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> OnScreenShotListener mListener;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">long</span> mStartListenTime;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 内部存储器内容观察者</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> MediaContentObserver mInternalObserver;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 外部存储器内容观察者</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> MediaContentObserver mExternalObserver;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 运行在 UI 线程的 Handler, 用于运行监听器回调</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> Handler mUiHandler = <span class="keyword">new</span> Handler(Looper.getMainLooper());</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="title">ScreenShotListenManager</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (context == <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;The context must not be null.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    mContext = context;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取屏幕真实的分辨率</span></span><br><span class="line">    <span class="keyword">if</span> (sScreenRealSize == <span class="keyword">null</span>) &#123;</span><br><span class="line">        sScreenRealSize = getRealScreenSize();</span><br><span class="line">        <span class="keyword">if</span> (sScreenRealSize != <span class="keyword">null</span>) &#123;</span><br><span class="line">            LogUtils.e(<span class="string">&quot;Screen Real Size: &quot;</span> + sScreenRealSize.x + <span class="string">&quot; * &quot;</span> + sScreenRealSize.y);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            LogUtils.e(<span class="string">&quot;Get screen real size failed.&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> ScreenShotListenManager <span class="title">newInstance</span><span class="params">(Context context)</span> </span>&#123;</span><br><span class="line">    assertInMainThread();</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> ScreenShotListenManager(context);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 启动监听</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">startListen</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    assertInMainThread();</span><br><span class="line"></span><br><span class="line">    sHasCallbackPaths.clear();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 记录开始监听的时间戳</span></span><br><span class="line">    mStartListenTime = System.currentTimeMillis();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建内容观察者</span></span><br><span class="line">    mInternalObserver = <span class="keyword">new</span> MediaContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI, mUiHandler);</span><br><span class="line">    mExternalObserver = <span class="keyword">new</span> MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mUiHandler);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注册内容观察者</span></span><br><span class="line">    mContext.getContentResolver().registerContentObserver(</span><br><span class="line">            MediaStore.Images.Media.INTERNAL_CONTENT_URI,</span><br><span class="line">            <span class="keyword">false</span>,</span><br><span class="line">            mInternalObserver</span><br><span class="line">    );</span><br><span class="line">    mContext.getContentResolver().registerContentObserver(</span><br><span class="line">            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,</span><br><span class="line">            <span class="keyword">false</span>,</span><br><span class="line">            mExternalObserver</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 停止监听</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">stopListen</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    assertInMainThread();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注销内容观察者</span></span><br><span class="line">    <span class="keyword">if</span> (mInternalObserver != <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">          mContext.getContentResolver().unregisterContentObserver(mInternalObserver);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        mInternalObserver = <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (mExternalObserver != <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">          mContext.getContentResolver().unregisterContentObserver(mExternalObserver);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        mExternalObserver = <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 清空数据</span></span><br><span class="line">    mStartListenTime = <span class="number">0</span>;</span><br><span class="line">    sHasCallbackPaths.clear();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 处理媒体数据库的内容改变</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">handleMediaContentChange</span><span class="params">(Uri contentUri)</span> </span>&#123;</span><br><span class="line">    Cursor cursor = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">// 数据改变时查询数据库中最后加入的一条数据</span></span><br><span class="line">        cursor = mContext.getContentResolver().query(</span><br><span class="line">                contentUri,</span><br><span class="line">                Build.VERSION.SDK_INT &lt; <span class="number">16</span> ? MEDIA_PROJECTIONS : MEDIA_PROJECTIONS_API_16,</span><br><span class="line">                <span class="keyword">null</span>,</span><br><span class="line">                <span class="keyword">null</span>,</span><br><span class="line">                MediaStore.Images.ImageColumns.DATE_ADDED + <span class="string">&quot; desc limit 1&quot;</span></span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (cursor == <span class="keyword">null</span>) &#123;</span><br><span class="line">            LogUtils.e(<span class="string">&quot;Deviant logic.&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!cursor.moveToFirst()) &#123;</span><br><span class="line">            LogUtils.e(<span class="string">&quot;Cursor no data.&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取各列的索引</span></span><br><span class="line">        <span class="keyword">int</span> dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);</span><br><span class="line">        <span class="keyword">int</span> dateTakenIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN);</span><br><span class="line">        <span class="keyword">int</span> widthIndex = -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> heightIndex = -<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> (Build.VERSION.SDK_INT &gt;= <span class="number">16</span>) &#123;</span><br><span class="line">            widthIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.WIDTH);</span><br><span class="line">            heightIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.HEIGHT);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取行数据</span></span><br><span class="line">        String data = cursor.getString(dataIndex);</span><br><span class="line">        <span class="keyword">long</span> dateTaken = cursor.getLong(dateTakenIndex);</span><br><span class="line">        <span class="keyword">int</span> width = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> height = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span> (widthIndex &gt;= <span class="number">0</span> &amp;&amp; heightIndex &gt;= <span class="number">0</span>) &#123;</span><br><span class="line">            width = cursor.getInt(widthIndex);</span><br><span class="line">            height = cursor.getInt(heightIndex);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// API 16 之前, 宽高要手动获取</span></span><br><span class="line">            Point size = getImageSize(data);</span><br><span class="line">            width = size.x;</span><br><span class="line">            height = size.y;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 处理获取到的第一行数据</span></span><br><span class="line">        handleMediaRowData(data, dateTaken, width, height);</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (cursor != <span class="keyword">null</span> &amp;&amp; !cursor.isClosed()) &#123;</span><br><span class="line">            cursor.close();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> Point <span class="title">getImageSize</span><span class="params">(String imagePath)</span> </span>&#123;</span><br><span class="line">    BitmapFactory.Options options = <span class="keyword">new</span> BitmapFactory.Options();</span><br><span class="line">    options.inJustDecodeBounds = <span class="keyword">true</span>;</span><br><span class="line">    BitmapFactory.decodeFile(imagePath, options);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Point(options.outWidth, options.outHeight);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 处理获取到的一行数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">handleMediaRowData</span><span class="params">(String data, <span class="keyword">long</span> dateTaken, <span class="keyword">int</span> width, <span class="keyword">int</span> height)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (checkScreenShot(data, dateTaken, width, height)) &#123;</span><br><span class="line">        LogUtils.e(<span class="string">&quot;ScreenShot: path = &quot;</span> + data + <span class="string">&quot;; size = &quot;</span> + width + <span class="string">&quot; * &quot;</span> + height</span><br><span class="line">                + <span class="string">&quot;; date = &quot;</span> + dateTaken);</span><br><span class="line">        <span class="keyword">if</span> (mListener != <span class="keyword">null</span> &amp;&amp; !checkCallback(data)) &#123;</span><br><span class="line">            mListener.onShot(data);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 如果在观察区间媒体数据库有数据改变，又不符合截屏规则，则输出到 log 待分析</span></span><br><span class="line">        LogUtils.e(<span class="string">&quot;Media content changed, but not screenshot: path = &quot;</span> + data</span><br><span class="line">                + <span class="string">&quot;; size = &quot;</span> + width + <span class="string">&quot; * &quot;</span> + height + <span class="string">&quot;; date = &quot;</span> + dateTaken);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断指定的数据行是否符合截屏条件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">checkScreenShot</span><span class="params">(String data, <span class="keyword">long</span> dateTaken, <span class="keyword">int</span> width, <span class="keyword">int</span> height)</span> </span>&#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * 判断依据一: 时间判断</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">// 如果加入数据库的时间在开始监听之前, 或者与当前时间相差大于10秒, 则认为当前没有截屏</span></span><br><span class="line">    <span class="keyword">if</span> (dateTaken &lt; mStartListenTime || (System.currentTimeMillis() - dateTaken) &gt; <span class="number">10</span> * <span class="number">1000</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * 判断依据二: 尺寸判断</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">if</span> (sScreenRealSize != <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="comment">// 如果图片尺寸超出屏幕, 则认为当前没有截屏</span></span><br><span class="line">        <span class="keyword">if</span> (!((width &lt;= sScreenRealSize.x &amp;&amp; height &lt;= sScreenRealSize.y)</span><br><span class="line">                || (height &lt;= sScreenRealSize.x &amp;&amp; width &lt;= sScreenRealSize.y))) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * 判断依据三: 路径判断</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">if</span> (TextUtils.isEmpty(data)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    data = data.toLowerCase();</span><br><span class="line">    <span class="comment">// 判断图片路径是否含有指定的关键字之一, 如果有, 则认为当前截屏了</span></span><br><span class="line">    <span class="keyword">for</span> (String keyWork : KEYWORDS) &#123;</span><br><span class="line">        <span class="keyword">if</span> (data.contains(keyWork)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 判断是否已回调过, 某些手机ROM截屏一次会发出多次内容改变的通知; &lt;br/&gt;</span></span><br><span class="line"><span class="comment"> * 删除一个图片也会发通知, 同时防止删除图片时误将上一张符合截屏规则的图片当做是当前截屏.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">checkCallback</span><span class="params">(String imagePath)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (sHasCallbackPaths.contains(imagePath)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 大概缓存15~20条记录便可</span></span><br><span class="line">    <span class="keyword">if</span> (sHasCallbackPaths.size() &gt;= <span class="number">20</span>) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            sHasCallbackPaths.remove(<span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    sHasCallbackPaths.add(imagePath);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取屏幕分辨率</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> Point <span class="title">getRealScreenSize</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Point screenSize = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        screenSize = <span class="keyword">new</span> Point();</span><br><span class="line">        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);</span><br><span class="line">        Display defaultDisplay = windowManager.getDefaultDisplay();</span><br><span class="line">        <span class="keyword">if</span> (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) &#123;</span><br><span class="line">            defaultDisplay.getRealSize(screenSize);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Method mGetRawW = Display.class.getMethod(<span class="string">&quot;getRawWidth&quot;</span>);</span><br><span class="line">                Method mGetRawH = Display.class.getMethod(<span class="string">&quot;getRawHeight&quot;</span>);</span><br><span class="line">                screenSize.set(</span><br><span class="line">                        (Integer) mGetRawW.invoke(defaultDisplay),</span><br><span class="line">                        (Integer) mGetRawH.invoke(defaultDisplay)</span><br><span class="line">                );</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                screenSize.set(defaultDisplay.getWidth(), defaultDisplay.getHeight());</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> screenSize;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 设置截屏监听器</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setListener</span><span class="params">(OnScreenShotListener listener)</span> </span>&#123;</span><br><span class="line">    mListener = listener;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">interface</span> <span class="title">OnScreenShotListener</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onShot</span><span class="params">(String imagePath)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">assertInMainThread</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (Looper.myLooper() != Looper.getMainLooper()) &#123;</span><br><span class="line">        StackTraceElement[] elements = Thread.currentThread().getStackTrace();</span><br><span class="line">        String methodMsg = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">if</span> (elements != <span class="keyword">null</span> &amp;&amp; elements.length &gt;= <span class="number">4</span>) &#123;</span><br><span class="line">            methodMsg = elements[<span class="number">3</span>].toString();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">&quot;Call the method must be in main thread: &quot;</span> + methodMsg);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 媒体内容观察者(观察媒体数据库的改变)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="class"><span class="keyword">class</span> <span class="title">MediaContentObserver</span> <span class="keyword">extends</span> <span class="title">ContentObserver</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Uri mContentUri;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MediaContentObserver</span><span class="params">(Uri contentUri, Handler handler)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(handler);</span><br><span class="line">        mContentUri = contentUri;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onChange</span><span class="params">(<span class="keyword">boolean</span> selfChange)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onChange(selfChange);</span><br><span class="line">        handleMediaContentChange(mContentUri);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>3、 调用 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">manager = ScreenShotListenManager.newInstance(ScreenshotsActivity.<span class="keyword">this</span>);</span><br><span class="line">manager.setListener(</span><br><span class="line">        <span class="keyword">new</span> ScreenShotListenManager.OnScreenShotListener() &#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onShot</span><span class="params">(String imagePath)</span> </span>&#123;</span><br><span class="line">                <span class="comment">// do something</span></span><br><span class="line">               </span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>参考：<br><a target="_blank" rel="noopener" href="https://blog.csdn.net/xietansheng/article/details/52692163">https://blog.csdn.net/xietansheng/article/details/52692163</a> </p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/%E6%88%AA%E5%B1%8F/" rel="tag"># 截屏</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/%E5%AE%89%E5%8D%93%E5%B0%8F%E6%8A%80%E5%B7%A7%EF%BC%9A%E8%BD%AF%E4%BB%B6%E4%B8%AD%E6%98%BE%E7%A4%BA%E7%89%88%E6%9C%AC%E6%89%93%E5%8C%85%E6%97%B6%E9%97%B4/" rel="prev" title="安卓小技巧：软件中显示版本打包时间">
      <i class="fa fa-chevron-left"></i> 安卓小技巧：软件中显示版本打包时间
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/%E5%AE%89%E5%8D%93%E6%89%AB%E7%A0%81%E6%9E%AA%E5%BC%80%E5%8F%91%EF%BC%8C%E6%8B%A6%E6%88%AA%E6%89%AB%E7%A0%81%E4%BA%8B%E4%BB%B6/" rel="next" title="安卓扫码枪开发，拦截扫码事件">
      安卓扫码枪开发，拦截扫码事件 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9C%80%E6%B1%82"><span class="nav-number">1.</span> <span class="nav-text">需求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E6%9E%90"><span class="nav-number">2.</span> <span class="nav-text">分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B7%E4%BD%93%E6%93%8D%E4%BD%9C"><span class="nav-number">3.</span> <span class="nav-text">具体操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%9D%83%E9%99%90"><span class="nav-number">3.1.</span> <span class="nav-text">1. 权限</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%88%9B%E5%BB%BA%E6%88%AA%E5%9B%BE%E7%9B%91%E5%90%AC%E5%99%A8"><span class="nav-number">3.2.</span> <span class="nav-text">2. 创建截图监听器</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-%E7%9B%91%E5%90%AC%E5%9B%BE%E5%BA%93"><span class="nav-number">3.2.1.</span> <span class="nav-text">2.1 监听图库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-%E7%89%B9%E5%AE%9A%E6%97%B6%E9%97%B4%E5%86%85"><span class="nav-number">3.2.2.</span> <span class="nav-text">2.2 特定时间内</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-%E7%AC%A6%E5%90%88%E8%A7%84%E5%88%99%E7%9A%84%E5%9B%BE%E7%89%87"><span class="nav-number">3.2.3.</span> <span class="nav-text">2.3 符合规则的图片</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E8%B0%83%E7%94%A8"><span class="nav-number">3.3.</span> <span class="nav-text">3. 调用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81%EF%BC%9A"><span class="nav-number">4.</span> <span class="nav-text">完整代码：</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
